<?php
namespace app\zhaosheng\controller;
use think\Controller;
use think\Db;

class ZhaoSheng extends Controller {
	
	public function index() {
		return $this->fetch();
	}
	
	public function query() {
		return $this->fetch();
	}

	public function upload() {
		return $this->fetch();
	}
	
	/**
	* thinkphp5 sql文档 https://www.kancloud.cn/manual/thinkphp5/135175
	*/
	public function getData() {
		//input获取参数
		$zqh = input('zqh');
		$where["中签号"] = $zqh;
		$data = Db::table("报名中签")
		->where($where)
		->find();//单条记录用find()多条记录用select()
		return findResult($data);
	}
	
	public function getList() {
		$data = Db::table("报名中签")
		->order("中签号 desc")
        ->page(1, 10) //分页查询第1页 10条记录 
		->select();//单条记录用find()多条记录用select()
		return selectResult($data);
	}

	public function uploadImg(){
		$zqh = request()->param('zqh');
        // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('image');
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    $info = $file
	    	->validate(['size'=>2097152,'ext'=>'jpg,gif,png,jpeg'])
	    	->move(ROOT_PATH . 'public' . DS . 'uploads'. DS . 'zhaosheng', $zqh.'.jpg');
	    if($info){
	    	$fileUrl = 'zhaosheng'.DS.$info->getSaveName();
	        // 成功上传后 获取上传信息
	        //echo $info->getExtension();输出图片后缀
	        //echo $info->getSaveName();输出图片路径
	        //echo $info->getFilename();输出图片名
	        Db::execute("update 报名中签 set 图片 = :imgage where 中签号 = :zqh",['imgage' => $fileUrl,'zqh' => $zqh]);
	        return array("code" => "000", "data" => $fileUrl);
	    }else{
	        // 上传失败获取错误信息
	        return array("code" => "999", "data" => $file->getError());
	    }
    }
	
}
